package com.example.demo.project.utils;

import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2015/6/29.
 */
public class WorkDayCounter {

    private List<Integer> REST_IN_WEEK = Arrays.asList(Calendar.SATURDAY, Calendar.SUNDAY);

    public WorkDayCounter() {

    }

    public WorkDayCounter(List<Integer> restDates) {
        this.REST_IN_WEEK = restDates;
    }

    public static WorkDayCounter getDefault() {
        return new WorkDayCounter();
    }

    public boolean includeWorkDay() {
        return REST_IN_WEEK.size() > 0;
    }

    public boolean isWorkDay(Calendar calendar) {
        if (!includeWorkDay()) return true;
        return !REST_IN_WEEK.contains(calendar.get(Calendar.DAY_OF_WEEK));
    }

    public boolean isWorkDay(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        return isWorkDay(c);
    }

    public int countWorkDay(Date beginDate, Date endDate) {
        boolean asc = beginDate.before(endDate);
        Calendar d1 = Calendar.getInstance();
        Calendar d2 = Calendar.getInstance();
        d1.setTime(asc ? beginDate : endDate);
        d2.setTime(!asc ? beginDate : endDate);
        int days = (int) ((d2.getTimeInMillis() - d1.getTimeInMillis()) / DateUtils.ONE_DAY_MS);
        int cycle = days / Calendar.DAY_OF_WEEK;
        int lessDays = days % Calendar.DAY_OF_WEEK;
        int workDays = cycle * (Calendar.DAY_OF_WEEK - REST_IN_WEEK.size()) + (isWorkDay(d1) ? 1 : 0);

        d1.add(Calendar.DAY_OF_YEAR, days - lessDays);
        for (; lessDays > 0; lessDays--) {
            d1.add(Calendar.DAY_OF_YEAR, 1);
            if (isWorkDay(d1)) {
                workDays += 1;
            }
        }
        return asc ? workDays : -workDays+1;
    }

    public Date nextWorkDate(Date beginDate) {
        Calendar c = Calendar.getInstance();
        c.setTime(beginDate);
        do {
            c.add(Calendar.DAY_OF_YEAR, 1);
        } while (!isWorkDay(c));
        return c.getTime();
    }

    /**
     * 计算某个日期加上数个工作日之后的日期
     * @param beginDate
     * @param days
     * @return
     */
    public Date addWorkDay(Date beginDate, int days) {
        if (0 > days) throw new IllegalStateException("工作日数不能小于0");
        Calendar c = Calendar.getInstance();
        c.setTime(beginDate);

        if (isWorkDay(c)) days--;
        int cycle = days / (Calendar.DAY_OF_WEEK - REST_IN_WEEK.size());
        int lessDays = days % (Calendar.DAY_OF_WEEK - REST_IN_WEEK.size());
        c.add(Calendar.DAY_OF_YEAR, cycle * Calendar.DAY_OF_WEEK);
        for (; lessDays > 0; lessDays--) {
            do {
                c.add(Calendar.DAY_OF_YEAR, 1);
            } while (!isWorkDay(c));
        }

        return c.getTime();
    }
    public Date addWorkDay1(Date beginDate, int days) {
        if (0 > days) throw new IllegalStateException("工作日数不能小于0");
        Calendar c = Calendar.getInstance();
        c.setTime(beginDate);

        int cycle = days / (Calendar.DAY_OF_WEEK - REST_IN_WEEK.size());
        int lessDays = days % (Calendar.DAY_OF_WEEK - REST_IN_WEEK.size());
        c.add(Calendar.DAY_OF_YEAR, cycle * Calendar.DAY_OF_WEEK);
        for (; lessDays > 0; lessDays--) {
            do {
                c.add(Calendar.DAY_OF_YEAR, 1);
            } while (!isWorkDay(c));
        }

        return c.getTime();
    }
}
